﻿Imports System.Data.SqlClient
Public Class ANTS
    Dim common_conn As New common
    Dim NewConString = ConnectDatabase.GetRegistryValue("stringcon")

    Private Sub ANTS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' AddNewNodeLabel.Text = Explorer1.TreeView.SelectedNode.Text
        'AddNewNodeLabel.ForeColor = Color.Maroon
    End Sub

    Private Sub Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ok.Click
        If TST_Text.Text = "" Then
            MsgBox("Test Suite Name cannot be empty!! Please enter valid name")
        ElseIf TSD_Text.Text = "" Then
            MsgBox("Test Suite Description cannot be empty!! Please enter valid description")
        Else
            ' Adds new node as a child node of the currently selected node.
            Dim newNode As TreeNode = New TreeNode(TST_Text.Text)
            Dim abc As String
            abc = "Insert into Tree_Views (Node_Title, Node_Description, Parent_Node, Node_Level) VALUES ('" & TST_Text.Text & "','" & TSD_Text.Text & "','" & Explorer1.TreeView.SelectedNode.Tag & "', '" & GetLastId() & "')"
            MsgBox(abc)

            Execute_commands(abc)
            'Dashboard.Load_view("select TOP 1 * from Tree_Views  order by  id Desc")
            'Dashboard.TreeView1.SelectedNode.Nodes.Add(newNode)
            'newNode.ImageIndex = 1
            'newNode.SelectedImageIndex = 2
            'newNode.Tag = TST_Text.Text
            'newNode.ContextMenuStrip = Dashboard.ContextMenuStrip_root
            common_conn.Load_trees()
            Explorer1.TreeView.TopNode.ExpandAll()
        End If
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click

    End Sub
    Public Function GetLastId()
        Dim conn As New SqlClient.SqlConnection
        ' todo: modify the connection string and include any
        ' additional required properties for your database.
        conn.ConnectionString = NewConString
        Dim myinsertquery As String = ("select Id from Tree_Views where Node_Title = '" & Explorer1.TreeView.SelectedNode.Text & "'")
        Dim mycommand As New SqlCommand(myinsertquery)
        mycommand.Connection = conn
        conn.Open()
        Dim reader As SqlDataReader = mycommand.ExecuteReader()
        While reader.Read()
            GetLastId = (reader(0).ToString)
        End While

    End Function

    Public Sub Execute_commands(ByVal command As String)
        Dim conn As New SqlClient.SqlConnection
        ' TODO: Modify the connection string and include any
        ' additional required properties for your database.
        conn.ConnectionString = NewConString
        Dim myInsertQuery As String = command
        Dim myCommand As New SqlCommand(myInsertQuery)
        myCommand.Connection = conn
        conn.Open()
        myCommand.ExecuteNonQuery()
        myCommand.Connection.Close()
    End Sub 'SelectSqlClientSrvRows


End Class

